package util;

import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Util{
	
	private static SimpleDateFormat f;
	private final static String TimeFormat1="yyyy.MM.dd";
	private static String password = "";
	private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
	
	public static String rundStr(String d,int len){
		String str = "";
		if(d != null){
			str = d;
			int ind = d.indexOf(".");
			if(d.length() > ind+1+len){
				str = d.substring(0,ind+1+len);
			}
		}
		return str;
	}
	
	/**
	 * 字符串截取
	 * @param str
	 * @param length
	 * @return
	 */
	public static String sub(String str,int  length){
		if(str != null && str.trim().length() > length){
			str = str.substring(0, length);
		}
		return str;
	}
	
	/**
	 * 将时间转换为字符串
	 * @param date
	 * @param gs
	 * @return
	 */
	public static String sdf(Date date,String gs){
		f = new SimpleDateFormat(gs);
		return f.format(date);
	}
	
	/**
	 * 将字符串转换为date
	 * @param dateStr
	 * @param gs
	 * @return
	 * @throws ParseException
	 */
	public static Date sdf(String dateStr,String gs) throws ParseException{
		f = new SimpleDateFormat(gs);
		return f.parse(dateStr);
	}
	
	/**
	 * uuid生成方法
	 * @return
	 */
	public static String uuid() {
		String uuid = "";
		SimpleDateFormat formatter = new SimpleDateFormat (TimeFormat1);
        Date dd = new Date();
        String dateString = formatter.format(dd);
		String TM = dateString;
		java.util.Random r = new java.util.Random();
		int RN = r.nextInt(10000);
		//int RX = r.nextInt(1000000);
		String NN = String.format("%04d", RN);
		//String XX = String.format("%04d", RX);
		uuid = TM + NN;
		uuid = replace(uuid, " ", "");
		uuid = replace(uuid, ".", "");
		uuid = replace(uuid, ":", "");
		return uuid;
	}

	/**
	 * uuid2生成方法 格式没有-
	 * @return
	 */
	public static String uuid2() {
		String uuid = "";
		SimpleDateFormat formatter = new SimpleDateFormat (TimeFormat1);
        Date dd = new Date();
        String dateString = formatter.format(dd);
		String TM = dateString;
		java.util.Random r = new java.util.Random();
		int RN = r.nextInt(1000);
		int RX = r.nextInt(1000000);
		String NN = String.format("%04d", RN);
		String XX = String.format("%04d", RX);
		uuid = TM + XX + NN;
		uuid = replace(uuid, " ", "");
		uuid = replace(uuid, ".", "");
		uuid = replace(uuid, ":", "");
		return uuid;
	}
	
	/**
	 * 替换
	 * @param str
	 * @param substr
	 * @param restr
	 * @return
	 */
	public static String replace(String str, String substr, String restr) {
		String[] tmp = split(str, substr);
		String returnstr = null;
		if (tmp.length != 0) {
			returnstr = tmp[0];
			for (int i = 0; i < tmp.length - 1; i++)
				returnstr = dealNull(returnstr) + restr + tmp[i + 1];
		}
		return dealNull(returnstr);
	}

	/**
	 * 截取
	 * @param source
	 * @param div
	 * @return
	 */
	public static String[] split(String source, String div) {
		int arynum = 0, intIdx = 0, intIdex = 0, div_length = div.length();
		if (source.compareTo("") != 0) {
			if (source.indexOf(div) != -1) {
				intIdx = source.indexOf(div);
				for (int intCount = 1;; intCount++) {
					if (source.indexOf(div, intIdx + div_length) != -1) {
						intIdx = source.indexOf(div, intIdx + div_length);
						arynum = intCount;
					} else {
						arynum += 2;
						break;
					}
				}
			} else
				arynum = 1;
		} else
			arynum = 0;

		intIdx = 0;
		intIdex = 0;
		String[] returnStr = new String[arynum];

		if (source.compareTo("") != 0) {

			if (source.indexOf(div) != -1) {

				intIdx = (int) source.indexOf(div);
				returnStr[0] = (String) source.substring(0, intIdx);

				for (int intCount = 1;; intCount++) {
					if (source.indexOf(div, intIdx + div_length) != -1) {
						intIdex = (int) source
								.indexOf(div, intIdx + div_length);

						returnStr[intCount] = (String) source.substring(intIdx
								+ div_length, intIdex);

						intIdx = (int) source.indexOf(div, intIdx + div_length);
					} else {
						returnStr[intCount] = (String) source.substring(intIdx
								+ div_length, source.length());
						break;
					}
				}
			} else {
				returnStr[0] = (String) source.substring(0, source.length());
				return returnStr;
			}
		} else {
			return returnStr;
		}
		return returnStr;
	}

	/**
	 * 去空
	 * @param str
	 * @return
	 */
	public static String dealNull(String str) {
		return str == null ? "" : str;
	}
	
	/**
	 * md5
	 * @param s
	 * @return
	 */
	public static String md5(String s) {
		password = s;
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
				'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
				'y', 'z' };
		try {
			byte[] strTemp = password.getBytes();
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(strTemp);
			byte[] b = md.digest();
			int j = b.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = b[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			System.out.print("Md5 ---------- getMd5");
			return null;
		}
	}
	
	private static String byteArrayToHexString(byte[] b) { 
		StringBuffer resultSb = new StringBuffer(); 
		for (int i = 0; i < b.length; i++) { 
			resultSb.append(byteToHexString(b[i])); 
		} 
		return resultSb.toString(); 
	} 

	private static String byteToHexString(byte b) { 
		int n = b; 
		if (n < 0) n = 256 + n; 
		int d1 = n / 16; 
		int d2 = n % 16; 
		return hexDigits[d1] + hexDigits[d2]; 
	} 

	public static String MD5Encode(String origin) { 
		String resultString = null; 
		try { 
			resultString=new String(origin); 
			MessageDigest md = MessageDigest.getInstance("MD5"); 
			resultString=byteArrayToHexString(md.digest(resultString.getBytes())); 
		} 
		catch (Exception ex) {
			ex.printStackTrace();
		} 
		return resultString; 
	}
}
